package middlewares

import (
	"GoScaffoldDemo/config"
	jwtutil "GoScaffoldDemo/utils/Jwtutil"
	"GoScaffoldDemo/utils/logsutil"

	"github.com/gin-gonic/gin"
)

func LoginVerify(c *gin.Context) {
	retrundata := config.NewReturnData()
	//得到全部申请信息
	requestPath := c.FullPath()
	//打印一下信息
	logsutil.Debug(map[string]interface{}{"请求路径": requestPath}, "")
	if requestPath == "/api/login" || requestPath == "/api/logout" { //在登录界面
		logsutil.Debug(map[string]interface{}{"请求路径": requestPath}, "登录和退出不需要验证")
		c.Next() //执行接下来的中间件
	} else { //不在登录界面
		// 获取token
		nowToken := c.Request.Header.Get("Authorization")
		if nowToken == "" { //token为空
			retrundata.Status = 200
			retrundata.Message = "没有携带token,请先登录"
			c.JSON(retrundata.Status, retrundata)
			c.Abort() //终止全部中间件
		} else { //有token
			//验证token是否合法
			claim, err := jwtutil.ParseToken(nowToken)
			if err != nil { //token验证未通过
				retrundata.Status = 401
				retrundata.Message = "token验证未通过"
				logsutil.Info(nil, "token验证未通过")
				c.JSON(retrundata.Status, retrundata)
			} else { //token验证通过
				logsutil.Info(nil, "token验证通过")
				c.Set("claim", claim)
			}

		}
	}

}
